<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <title>Egret</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="full-screen" content="true" />
    <meta name="screen-orientation" content="portrait" />
    <meta name="x5-fullscreen" content="true" />
    <meta name="360-fullscreen" content="true" />
    <style>
        html, body {
            -ms-touch-action: none;
            background: #888888;
            padding: 0;
            border: 0;
            margin: 0;
            height: 100%;
        }
    </style>

     <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css">
     <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
</head>

<body>
    <div style="margin: auto;width: 100%;height: 100%;" class="egret-player"
         data-entry-class="Main"
         data-orientation="auto"
         data-scale-mode="showAll"
         data-frame-rate="30"
         data-content-width="1280"
         data-content-height="768"
         data-show-paint-rect="false"
         data-multi-fingered="2"
         data-show-fps="false" data-show-log="false"
         data-show-fps-style="x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9">
    </div>
    <div id="code" style="border: #000 solid 1px; padding: 2rem; display: none" title="人物代码">
        
        <textarea name="code1" id="code1-area" cols="30" rows="10" style="font-family: Consolas, monospace">
function play1(m) {
    m.moveDown();
    m.putBoom();
}
        </textarea>

        <textarea name="code2" id="code2-area" cols="30" rows="10" style="font-family: Consolas, monospace">
function play2(m) {
    m.moveDown();
    m.putBoom();
}
        </textarea>
        <button class="button" id="sub">提交</button>
    </div>

    <button id="showCode">人物代码框</button>
<script>
    var loadScript = function (list, callback) {
        var loaded = 0;
        var loadNext = function () {
            loadSingleScript(list[loaded], function () {
                loaded++;
                if (loaded >= list.length) {
                    callback();
                }
                else {
                    loadNext();
                }
            })
        };
        loadNext();
    };

    var loadSingleScript = function (src, callback) {
        var s = document.createElement('script');
        s.async = false;
        s.src = src;
        s.addEventListener('load', function () {
            s.parentNode.removeChild(s);
            s.removeEventListener('load', arguments.callee, false);
            callback();
        }, false);
        document.body.appendChild(s);
    };

    var xhr = new XMLHttpRequest();
    xhr.open('GET', './manifest.json?v=' + Math.random(), true);
    xhr.addEventListener("load", function () {
        var manifest = JSON.parse(xhr.response);
        var list = manifest.initial.concat(manifest.game);
        loadScript(list, function () {
            /**
             * {
             * "renderMode":, //引擎渲染模式，"canvas" 或者 "webgl"
             * "audioType": 0 //使用的音频类型，0:默认，2:web audio，3:audio
             * "antialias": //WebGL模式下是否开启抗锯齿，true:开启，false:关闭，默认为false
             * "retina": //是否基于devicePixelRatio缩放画布
             * }
             **/
            egret.runEgret({ renderMode: "webgl", audioType: 0 });
        });
    });
    xhr.send(null);
</script>
<script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script>
/**
 * 发送事件. index是第几个炸弹人
 */
    function sendCode(code, index) {
        let e = new CustomEvent("onCommitCode", {
            detail: {
                index: index,
                code: eval("(" + code + ")")
            }
        });
        window.dispatchEvent(e);
    }

    $("#showCode").click(function (event) {
        $('#code').dialog({
            width: 800
        });
    })


    $('#sub').on('click', () => {
        let code1 = $('#code1-area').val();
        let code2 = $('#code2-area').val();
        sendCode(code1, 0);
        sendCode(code2, 1);
    });
</script>
</body>

</html>